//
//  ZMAlertView.m
//  ZMKitDemo
//
//  Created by zm on 2018/6/13.
//  Copyright © 2018年 zm. All rights reserved.
//

#import "ZMAlertView.h"

@implementation ZMAlertView

+ (void)alert:(nullable NSString *)title
      message:(NSString *)message
 dismissTitle:(NSString *)dismissTitle
viewController:(UIViewController *)viewController
dismissAction:(void (^ _Nullable)(id _Nullable ret))dismissAction {
    
    dispatch_main_async_safe(^{
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:(UIAlertControllerStyleAlert)];
        UIAlertAction *action = [UIAlertAction actionWithTitle:dismissTitle style:UIAlertActionStyleDefault handler:dismissAction];
        [alertController addAction:action];
        
        [viewController presentViewController:alertController animated:YES completion:nil];
    });

}

+ (void)confirmOrCancel:(nullable NSString *)title
                message:(NSString *)message
           confirmTitle:(NSString *)confirmTitle
            cancelTitle:(NSString *)cancelTitle
         viewController:(UIViewController *)viewController
          confirmAction:(void (^ _Nullable)(id _Nullable ret))confirmAction
          dismissAction:(void (^ _Nullable)(id _Nullable ret))dismissAction {
    
    dispatch_main_async_safe(^{
        
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:(UIAlertControllerStyleAlert)];
        UIAlertAction *action = [UIAlertAction actionWithTitle:cancelTitle style:UIAlertActionStyleCancel handler:dismissAction];
        [alertController addAction:action];
        
        UIAlertAction *confirmaction = [UIAlertAction actionWithTitle:confirmTitle style:UIAlertActionStyleDefault handler:confirmAction];
        [alertController addAction:confirmaction];
        
        [viewController presentViewController:alertController animated:YES completion:nil];
        
    });
    
}

+ (void)confirmOrCancelWithTextField:(nullable NSString *)title
                             message:(NSString *)message
                        confirmTitle:(NSString *)confirmTitle
                         cancelTitle:(NSString *)cancelTitle
                      viewController:(UIViewController *)viewController
                       confirmAction:(void (^ _Nullable)(NSString * _Nullable text))confirmAction                    dismissAction:(void (^ _Nullable)(id _Nullable ret))dismissAction
                configurationHandler:(void (^ _Nullable)(UITextField *textField))configurationHandler
{
    
    dispatch_main_async_safe(^{
        
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:(UIAlertControllerStyleAlert)];
        [alertController addTextFieldWithConfigurationHandler:configurationHandler];
        
        UIAlertAction *action = [UIAlertAction actionWithTitle:cancelTitle style:UIAlertActionStyleCancel handler:dismissAction];
        [alertController addAction:action];
        
        UIAlertAction *confirmaction = [UIAlertAction actionWithTitle:confirmTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            NSArray *tempArr = alertController.textFields;
            UITextField *tempTextField = tempArr[0];
            
            confirmAction(tempTextField.text);
        }];
        
        [alertController addAction:confirmaction];
        
        
        [viewController presentViewController:alertController animated:YES completion:nil];
        
    });
    
}
@end
